#!/bin/sh

command -v cygpath >/dev/null 2>&1
on_cygwin=$(( $? == 0 ? 1 : 0 ))

# below, we use non-environment variables to avoid implicitly updating the
# environment with UNIX versions of paths.

if [ "$on_cygwin" = 1 ] && [ "$JAVA" ]; then
	java=$(cygpath --unix "$JAVA")
else
	java=$JAVA
fi

if [ "$on_cygwin" = 1 ] && [ "$JAVA_HOME" ]; then
	java_home=$(cygpath --unix "$JAVA_HOME")
else
	java_home=$JAVA_HOME
fi

# honor JAVA environment variable if set; if not, use JAVA_HOME (if it's set);
# if not, fall back to the old hardcoded default.
: "${java:=${java_home:-/usr}/bin/java}"

# Follow symlinks, if we have readlink available; this will allow a symlink to
# the real wrapper script's location to be installed somewhere on the PATH (ie.
# /usr/local/bin/abe, or $HOME/bin/abe).
#
# This technique has flaws. See http://mywiki.wooledge.org/BashFAQ/028 to
# understand its limitations before reusing it in your own scripts.
executable=$0
if command -v readlink >/dev/null 2>&1; then
	while target=$(readlink "$executable"); do
		[ "$executable" = "$target" ] && break # avoid endless loops
		executable=$target
	done
fi
basedir=$(dirname "$executable")

cp=$basedir:$basedir/bin:$basedir/abe.jar
for f in $basedir/lib/*.jar; do
	# note that this technique only works as long as nullglob is disabled
	# (which it is, by default, on all POSIX shells); we're relying on the
	# *.jar glob expression returning literally '*.jar' as a result when no
	# matches exist on-disk.
	[ -e "$f" ] || {
		echo "WARNING: No jars found in lib directory. Please download a BouncyCastle implementation per the README (looking for: $f)" >&2
		continue
	}
	cp=$cp:$f
done

[ -e "$basedir/abe.jar" ] || {
	echo "ERROR: Could not find abe.jar in $basedir; did you run ant to compile?" >&2
	exit 1
}

[ -e "$java" ] || {
	echo "ERROR: Could not find java executable at $java" >&2
	exit 1
}

if [ "$on_cygwin" = 1 ]; then
	# convert classpath from UNIX to Windows paths
	unix_cp=$cp
	IFS=:
	set -f
	cp=
	for element in $unix_cp; do
		[ "$element" ] || continue
		cp="$cp;$(cygpath --windows "$element")"
	done
fi

# Using "exec" here replaces the script's process table entry with that of the
# JVM, avoiding the need to keep the script in memory while the executable
# runs.
exec "$java" -cp "$cp" org.nick.abe.Main "$@"
